﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Evolution
{
    class Evo {
        private int w, h;// width and height of the field
        private byte[,] mas; // array, that describes the field
        private byte[,] masNew;// array, that describes the field after iteration
        public Evo(int w,int h)
        // Constructor, that takes width and height o the field
        { 
            this.w = w;
            this.h = h;
            mas = new byte [h,w];
            masNew = new byte[h, w];
            Random rnd = new Random();
            for (int i = 0; i < h; i++)
                for (int j = 0; j < w; j++)
                    mas[i, j] = (byte)rnd.Next(2);

            Print();
        }
        public void Print()
       // Displays the field in Console
        { 
            for (int i = 0; i < h; i++)
                for (int j = 0; j < w; j++)
                {
                    if (mas[i, j] == 1)
                        Console.Write('*');
                    else
                        Console.Write(' ');
                    if (j == w - 1)
                        Console.WriteLine();
                }
            
        }

        private byte CheckCell(int i, int j, ref byte none)
        /* Checks, if there is a live cell in position (i,j). 
         * If it is, returns 1, else returns 0;
         * If cell is outmost, adds 1 to parameter none;
         * */
        {
            if (i < 0 || j < 0 || i > h - 1 || j > w - 1)
            {
                none += 1;
                return 0;
            }
            else
            {
                if (mas[i, j] == 1)
                    return 1;
                else
                    return 0;
            }


        
        }

        public void MakeEvo()
        /*
         * Evolves the field. 
         */
        {
            byte count = 0; // Number of live cells near the cell in position (i,j)
            byte none = 0; // For outmost cells, the number of near cells over the border
            for (int i = 0; i < h; i++)
                for (int j = 0; j < w; j++)
                {                
                    count = 0;
                    none = 0;
                    count += CheckCell(i - 1, j - 1, ref none);
                    count += CheckCell(i - 1, j, ref none);
                    count += CheckCell(i - 1, j + 1, ref none);
                    count += CheckCell(i + 1, j - 1, ref none);
                    count += CheckCell(i + 1, j, ref none);
                    count += CheckCell(i + 1, j + 1, ref none);
                    count += CheckCell(i, j - 1, ref none);
                    count += CheckCell(i, j + 1, ref none);
                    if (mas[i, j] == 0)
                    {
                        if (count >= 2)
                            masNew[i, j] = 1;
                    }
                    else
                        if (count + none == 8)
                            masNew[i, j] = 0;
                        else
                            masNew[i, j] = 1;

                }

            for (int i = 0; i < h; i++)
                for (int j = 0; j < w; j++)
                    mas[i, j] = masNew[i, j];
        }
                    


    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WindowHeight = 35;
            Console.Title = "Evolution";
            Evo evo = new Evo(30,30);
            Console.WriteLine("Press Space to Evolve or other key to exit!");
                      
            while (Console.ReadKey().KeyChar==' ' ) // if key is Space, continue
            {
                Console.Clear();//Clear Console
                evo.MakeEvo();  //Evolve
                evo.Print();    //Display
                Console.WriteLine("Press Space to Evolve or other key to exit!");              
            }

            Console.Clear();
            evo.Print();
            Console.WriteLine("Evolution is over! =)");
            Console.ReadLine();
           
        }
    }
}
